package com.ebeijia.controller;

import com.ebeijia.common.FileType;
import com.ebeijia.context.RequestResponseContext;
import com.ebeijia.util.LoggerUtil;
import com.ebeijia.util.PropertiesUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.UUID;

/**
 * Created by YPJ on 2016/5/18.
 */
@RequestMapping("/file")
@Controller
public class UploadController {
    private String relativePath = "/image/";

    @ResponseBody
    @RequestMapping(value = "/ckeditor/uploadFile.json")
    public void uploadFile(@RequestParam("upload") MultipartFile file,HttpSession session){
        RequestResponseContext.getResponse().setContentType("text/html;charset=UTF-8");
        
         String host=PropertiesUtils.getProperties("image.host");
         String imagePath=PropertiesUtils.getProperties("image.upload.path");
        String bucketType = RequestResponseContext.getRequest().getParameter("bucketType");
        String CKEditorFuncNum =  RequestResponseContext.getRequest().getParameter("CKEditorFuncNum");
        if (file == null || file.isEmpty()) {
        LoggerUtil.error("upload file error, file is empty");
        }
        String fileName = file.getOriginalFilename();
        String contentType = file.getContentType();

        FileType fileType = FileType.getFileType(contentType);
        PrintWriter out;
        if (fileType.name().equals(FileType.IMAGE.name())) {
            LoggerUtil.info("upload file, user:" + RequestResponseContext.getUserContext().getAccount() + ", fileName:" + fileName + ", contentType:" + contentType + ", size:"
                    + file.getSize());
            String url = "";
            try {
                String fileNameSuf = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);

                String tempFileName = UUID.randomUUID().toString() + "." + fileNameSuf;
                String direct=imagePath + relativePath;
                File dir = new File(direct);
                if (!dir.isDirectory()) {
                    dir.mkdirs();
                }

                File tmp = new File(direct+ tempFileName);
                file.transferTo(tmp);
                url = host + relativePath + tempFileName;
            } catch (Exception e) {
                LoggerUtil.error("upload file error", e);
            }

            String result = "<script type=\"text/javascript\">window.parent.CKEDITOR.tools.callFunction(" + CKEditorFuncNum + ", '" + url
                    + "');</script>";
            try {
                out = RequestResponseContext.getResponse().getWriter();
                out.print(result);
                out.flush();
            } catch (IOException e) {
                LoggerUtil.error("PrintWriter print error", e);
            }
        } else {
            String result = "<script type=\"text/javascript\">window.parent.CKEDITOR.tools.callFunction(" + CKEditorFuncNum + ",'',"
                    + "'*文件格式不正确（必须为.jpg/.gif/.bmp/.png文件）')</script>";
            try {
                out = RequestResponseContext.getResponse().getWriter();
                out.print(result);
                out.flush();
            } catch (IOException e) {
                LoggerUtil.error("PrintWriter print error", e);
            }
        }
    }

}
